/**
 * 货物仓位数据
 */


//货物编辑
function cwOne(v){
    var hh = '';
    var formData = {
        cw_id: v,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwcw/index',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i, hwList, ii;
            h += '<style type="text/css">.header { font-size: 14px; } .thr { margin-bottom: 5px; padding: 5px; width: 95%; background: #00aaff; color: #555; border: 1px solid #0088CC; border-radius: 5px; font-size: 14px; } .group { margin: 10px 0; padding: 5px; border: 1px solid #ddd; border-radius: 5px; } .title { font-size: 14px; color: #888; } .caozuo { color: 888; font-size: 14px; } .caozuo span { margin: 0 10px; font-size: 14px; color: #0088CC; cursor: pointer; } .caozuo .cw_num { width: 30px; border: 2px solid #0088CC; text-align: center; } .tobtn { padding: 0 5px; color: #fff; text-shadow: 0 0 2px #0088CC; cursor: pointer; float: right; } .del { color: red; }</style>';
            h += '<div class="header">→货物详情</div>';
            for (i = 0; i < d.length; i ++) {
                h += '<div class="thr">仓位编号：';
                h += d[i].title;
                h += '<span class="tobtn" onclick="popHwAdd(' + d[i].id + ');">货物入仓</span>';
                h += '</div>';
                hwList = d[i].hwList;
                if (hwList.length > 0) {
                    for (ii = 0; ii < hwList.length; ii ++) {
                        h += '<div class="group">';
                        h += '<div class="title">货物名称：' + hwList[ii].title + '</div>';
                        h += '<div class="title">货物编码：' + hwList[ii].hw_code + '</div>';
                        h += '<div class="title">现有库存：' + hwList[ii].stock + '</div>';
                        h += '<div class="caozuo">更新库存：';
                        h += '<span onclick="cangDecre(' + hwList[ii].id + ');">减仓</span>';
                        h += '<input type="text" class="cw_num" id="cw_num" value="1"/>';
                        h += '<span onclick="cangIncre(' + hwList[ii].id + ');">加仓</span>';
                        h += '</div>';
                        h += '</div>';
                    }
                }
            }
            $('#tlist').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//修改货物HTML
function popHwAdd(cw_id){
    var h = '';
    h += '<style type="text/css">.popCnt { height: 340px; } .popCnt .title { margin-bottom: 30px; text-align: center; } .popCnt table { width: 100%; border: 1px solid #ccc; box-shadow: 0 0 5px #ccc; } .popCnt td { border: 1px solid #eee; text-align: center; } .popCnt input { border-bottom: 1px solid #0088CC; } .popBtn { padding: 5px 10px; color: #0088CC; float: right; } </style>';
    h += '<div class="popMask" onclick="$(\'#formPop\').html(\'\');"></div>';
    h += '<div class="popCnt">';
    h += '<div class="title">货物选择</div>';
    h += '<div style="margin: 5px 0;">货物号</div>';
    h += '<select style="width: 80%; border: 1px solid #ccc;" id="hw_id" onchange="hwOne();">';
    h += '<option>-选择货物号-</option>';
    h += '</select>';
    h += '<a href="/map/hwzd.html" class="popBtn">配置货物</a>';
    h += '<input type="hidden" id="cw_id0" value="' + cw_id + '"/>';
    h += '<table style="margin-top: 10px;">';
    h += '<tr>';
    h += '<td>货物名称</td>';
    h += '<td id="hw_name">xxx</td>';
    h += '</tr>';
    h += '<tr>';
    h += '<td>货物号</td>';
    h += '<td id="hw_code">xxx</td>';
    h += '</tr>';
    h += '<tr>';
    h += '<td>入仓数量</td>';
    h += '<td><input type="text" id="hw_num" placeholder="输入数量"/></td>';
    h += '</tr>';
    h += '<tr>';
    h += '<td>图片预览</td>';
    h += '<td id="hw_img"></td>';
    h += '</tr>';
    h += '</table>';
    h += '<button class="close" onclick="$(\'#formPop\').html(\'\');">取消</button>';
    h += '<button onclick="cangIn(' + cw_id + ');">入仓</button>';
    h += '</div>';
    $('#formPop').html(h);
    hwOption();
}


// 选项
function hwOption(){
    var formData = {
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwcw/option',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                return
            }
            var hw = res.data.hw;
            var h = '', i;
            h = '<option value="0">-选择货物号-</option>';
            for (i = 0; i < hw.length; i ++) {
                h += '<option value="' + hw[i].id + '">';
                h += hw[i].title;
                h += '</option>';
            }
            $('#hw_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


// 详情
function hwOne(){
    var id = $('#hw_id').val();
    var formData = {
        id: id,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwcw/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            $('#hw_name').html(d.title);
            $('#hw_code').html(d.hw_code);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


// 入仓
function cangIn(cw_id){
    var hw_code = $('#hw_code').html();
    var hw_num = $('#hw_num').val();
    var formData = {
        cw_id: cw_id, hw_code: hw_code,
        num: hw_num,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwcw/cang-in',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


// 减仓
function cangDecre(id){
    var cw_num = $('#cw_num').val();
    var formData = {
        id: id, num: cw_num,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwcw/cang-decre',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


// 加仓
function cangIncre(id){
    var cw_num = $('#cw_num').val();
    var formData = {
        id: id, num: cw_num,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwcw/cang-incre',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}